﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BHXH.Repository;
namespace Lists
{
    public class CoQuan : DoiTuong
    {
        #region Ten property
        public string Ten
        {
            get
            {
                if (_DataRow["Ten"] is string) return (string)_DataRow["Ten"];
                return "";
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region Sonha_Pho property
        public string Sonha_Pho
        {
            get
            {
                if (_DataRow["Sonha_Pho"] is string) return (string)_DataRow["Sonha_Pho"];
                return "";
            }
            set
            {
                _DataRow["Sonha_Pho"] = value;
            }
        }
        #endregion

        #region PhuongXa property
        public string PhuongXa
        {
            get
            {
                if (_DataRow["PhuongXa"] is string) return (string)_DataRow["PhuongXa"];
                return "";
            }
            set
            {
                _DataRow["PhuongXa"] = value;
            }
        }
        #endregion

        #region QuanHuyen property
        public QuanHuyen QuanHuyen
        {
            get
            {
                if (_DataRow["QuanHuyenID"] is Guid)
                    return (QuanHuyen)DanhMucQuanHuyen.Instance.GetByID((Guid)_DataRow["QuanHuyenID"]);
                else return null;
            }
            set
            {
                if (value is QuanHuyen)
                    _DataRow["QuanHuyenID"] = value.ID;
                else
                    _DataRow["QuanHuyenID"] = DBNull.Value;
            }
        }
        #endregion

        #region Tinh property
        public Tinh Tinh
        {
            get
            {
                if (_DataRow["TinhID"] is Guid)
                    return (Tinh)DanhMucTinh.Instance.GetByID((Guid)_DataRow["TinhID"]);
                else return null;
            }
            set
            {
                if (value is Tinh)
                    _DataRow["TinhID"] = value.ID;
                else
                    _DataRow["TinhID"] = DBNull.Value;
            }
        }
        #endregion

        #region DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        #region Fax property
        public string Fax
        {
            get
            {
                if (_DataRow["Fax"] is string) return (string)_DataRow["Fax"];
                return "";
            }
            set
            {
                _DataRow["Fax"] = value;
            }
        }
        #endregion
       
        public CoQuan(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucCoQuan : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucCoQuan _Instance;
        public static DanhMucCoQuan Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucCoQuan();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "CoQuan";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new CoQuan((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public CoQuan this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new CoQuan(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public CoQuan NewDoiTuong()
        {
            return new CoQuan(DataTable.NewRow());
        }

        public CoQuan GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new CoQuan(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public CoQuan GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucCoQuan GetAll()
        {
            return Instance;
        }

        public static DanhMucCoQuan Filter(string filterExpression)
        {
            try
            {
                DanhMucCoQuan obj = new DanhMucCoQuan();
                obj.DataView = new DataView(DanhMucCoQuan.Instance.DataTable, filterExpression, "Ten", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static CoQuan GetByTen(string ten)
        {
            try
            {
                return DanhMucCoQuan.Instance.GetByProperty("Ten", ten.Trim());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static CoQuan GetByMa(string ma)
        {
            try
            {
                return DanhMucCoQuan.Instance.GetByProperty("Ma", ma.Trim());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
